package com.ityang.hhhh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ityang.hhhh.conm.BaseContext;
import com.ityang.hhhh.conm.R;
import com.ityang.hhhh.domain.AddressBook;
import com.ityang.hhhh.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 地址簿管理
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 地址簿新增
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        log.info("地址簿新增 {}",addressBook);
        Long userId = addressBook.getUserId();
        addressBook.setUserId(userId);
        //创建时间
        addressBook.setCreateTime(LocalDateTime.now());
        //更新时间
        addressBook.setUpdateTime(LocalDateTime.now());
        //创建人
        addressBook.setCreateUser(userId);
        //修改人
        addressBook.setUpdateUser(userId);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }

    /**
     * 查询指定用户的全部地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(addressBook.getUserId());
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(addressBook.getUserId() != null,AddressBook::getUserId,addressBook.getUserId());
        lqw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper();
        luw.eq(AddressBook::getUserId,addressBook.getUserId())
            .set(AddressBook::getIsDefault,0);

        addressBookService.update(luw);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getByID(@PathVariable String id){
        AddressBook serviceById = addressBookService.getById(id);
        if (serviceById != null){

            return R.success(serviceById);
        }
        return R.error("查询失败");
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     * 根据ids删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        boolean removeByIds = addressBookService.removeByIds(ids);
        if (removeByIds){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 根据id修改
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){

        //更新时间
        addressBook.setUpdateTime(LocalDateTime.now());

        //修改人
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        boolean updateById = addressBookService.updateById(addressBook);
        if (updateById){
            return R.success("修改成功");
        }
        return R.success("修改失败");
    }
}
